<?php
/**
 * tvb log sqlite data
==================================
<appender>mq</appender>
<mq>
	<out_class>MemQueue</out_class>
	<pattern>SimpleText</pattern>
	<queue_name>test</queue_name>
	<buffer_size>10</buffer_size>
	<buffer_count>100</buffer_count>
	<log_path>/tmp/logs/</log_path>
	<mqlog_path>/tmp/mqlogs/</mqlog_path>
	<mem_server>vm032.dev.hk3.tvb.com:11215</mem_server>
	<mem_server>vm032.dev.hk3.tvb.com:11216</mem_server>
</mq>
==================================
 * @author william hu<william.hu@achievo.com>
 * @version 1.1
 * @package TVBLog
*/

require_once 'TVB/Logger/Appender/Interface.php';
require_once 'pworks/common/log/SystemLog.class.php' ;
require_once 'pworks/common/log/LogEntity.class.php' ;
require_once 'pworks/common/log/Logs.class.php' ;
require_once 'pworks/common/log/StdLog.class.php' ;

require_once 'TVB/memcachequeue/IMemcacheQueueClient.iface.php';
require_once 'TVB/memcachequeue/AbsDefaultMQClient.class.php';
require_once 'TVB/memcachequeue/MemcacheQueue.class.php';
require_once 'TVB/memcachequeue/MemcacheQueueMessage.class.php';
require_once 'TVB/memcachequeue/sendMessage.class.php';

class TVB_Logger_Appender_MemQueue implements TVB_Logger_Appender_Interface
{
    //implement the write function 
    public function write($log_entity)
    {
		//get log entiry array
		$data_info 		= TVB_Logger_Config::get('mq', $log_entity->category);
		$queue_name 	= $data_info['queue_name'];
		$buffer_size 	= isset($data_info['buffer_size'])?$data_info['buffer_size']:10;
		$buffer_count	= isset($data_info['buffer_count'])?$data_info['buffer_count']:100;
		$host_group  	= (array) $data_info['mem_server'];
		$log_path		= isset($data_info['log_path'])?$data_info['log_path']:'/tmp/logs/';
		$mqlog_path		= isset($data_info['mqlog_path'])?$data_info['mqlog_path']:'/tmp/mqlogs/';
		$data_info['log_file'] =  $log_path.date('Ymd').'.log';
		$data_info['log_path'] =  $log_path;
		
		$timeout 		= 10;
		$t1 = microtime();
		$when = date('Y-m-d H:i:s', strtotime($log_entity->when));
		require_once 'TVB/Logger/Pattern.php';
		$pattern = TVB_Logger_Pattern::get($data_info['pattern']);
		$messageValue = $pattern->toString($log_entity);
		$t2 = microtime();
		$sysLog = new SystemLog();
		
		$logfileName = $mqlog_path.date("Ymd").'_'.$queue_name.'_client_buf.log';
		if (!file_exists($logfileName )) {
			@mkdir($mqlog_path, 0777, true);
			file_put_contents($logfileName ,'');
		}
		$sysLog->setLogFile($logfileName);
		$logs = new Logs();
		$logs->_logs[] = $sysLog; // to log file
		//$logs->_logs[] = new StdLog(); // to console

		MemcacheQueue::$logs = $logs;
		$t3 = microtime();
		foreach ($host_group as $hosts) {
			$tmp = explode(":", $hosts);
			$host = $tmp[0];
			$port = (isset($tmp[1]))?$tmp[1]:'11211';
			MemcacheQueue::addServer($host, $port);
		}
		$t4 = microtime();
		MemcacheQueue::setQueue($queue_name, $buffer_size, $buffer_count, $timeout); 

		//for client side
		$client = MemcacheQueue::getClientInstance(new SimpleMQClient());
		$message = new MemcacheQueueMessage();
		$data['mq_info'] = $data_info;
		$data['message'] = $messageValue;
		$message->data = json_encode($data);
		
		$t5 = microtime();
		$result =  $client->sendMessage($message);
		/**
		$a1 = $t5-$t4;
		$a2 = $t4-$t3;
		$a3 = $t3-$t2;
		$a4 = $t2-$t1;
		echo "<br>t5-t4:".$a1."<br>";
		echo "<br>t4-t3:".$a2."<br>";
		echo "<br>t3-t2:".$a3."<br>";
		echo "<br>t2-t1:".$a4."<br>";
		**/
		//return $result;
		return true;
    }

}
?>